﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using xna = Microsoft.Xna.Framework;
using URWPGSim2D.Common;
using URWPGSim2D.StrategyLoader;
using URWPGSim2D.FishDecision;
namespace URWPGSim2D.Strategy
{
    public class Strategy : MarshalByRefObject, IStrategy
    {
        #region reserved code never be changed or removed
        /// <summary>
        /// override the InitializeLifetimeService to return null instead of a valid ILease implementation
        /// to ensure this type of remote object never dies
        /// </summary>
        /// <returns>null</returns>
        public override object InitializeLifetimeService()
        {
            //return base.InitializeLifetimeService();
            return null; // makes the object live indefinitely
        }
        #endregion

        /// <summary>
        /// 决策类当前对象对应的仿真使命参与队伍的决策数组引用 第一次调用GetDecision时分配空间
        /// </summary>
        private Decision[] decisions = null;

        /// <summary>
        /// 获取队伍名称 在此处设置参赛队伍的名称
        /// </summary>
        /// <returns>队伍名称字符串</returns>
        public string GetTeamName()
        {
            return "Venus Star j Team";
        }

        /// <summary>
        /// 获取当前仿真使命（比赛项目）当前队伍所有仿真机器鱼的决策数据构成的数组
        /// </summary>
        /// <param name="mission">服务端当前运行着的仿真使命Mission对象</param>
        /// <param name="teamId">当前队伍在服务端运行着的仿真使命中所处的编号 
        /// 用于作为索引访问Mission对象的TeamsRef队伍列表中代表当前队伍的元素</param>
        /// <returns>当前队伍所有仿真机器鱼的决策数据构成的Decision数组对象</returns>
        public Decision[] GetDecision(Mission mission, int teamId)
        {
            // 决策类当前对象第一次调用GetDecision时Decision数组引用为null
            if (decisions == null)
            {// 根据决策类当前对象对应的仿真使命参与队伍仿真机器鱼的数量分配决策数组空间
                decisions = new Decision[mission.CommonPara.FishCntPerTeam];
            }

           
            int balljindong = FishDecision.FishDecision.Panduanballjindong(mission.EnvRef.Balls[0].PositionMm);
            if (balljindong == 0)
            {
                decisions[0].VCode = 8;
                decisions[0].TCode = 4;
                if (FishDecision.FishDecision.Distance(mission.TeamsRef[0].Fishes[0].PositionMm, mission.EnvRef.ObstaclesRound[2].PositionMm) < 600 && FishDecision.FishDecision.Distance(mission.TeamsRef[0].Fishes[0].PositionMm, mission.EnvRef.ObstaclesRound[2].PositionMm) > 450)
                {
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(1032, 0, 220), 0);
                    //decisions[0].VCode = 8;
                    //decisions[0].TCode = 3;
                }

                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 2)
                {
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(20, 0, -260), 0);
                    // decisions[0].VCode = 8;
                    // decisions[0].TCode = 14; ;
                }

                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 3)
                {
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(20, 0, -260), 0);
                    //decisions[0].VCode = 6;
                    //decisions[0].TCode = 0;
                }
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 4)
                {
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(-1000, 0, 270), 0);
                    //decisions[0].VCode = 7;
                    //decisions[0].TCode = 2;
                }
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 5)
                {
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(-1000, 0, 270), 0);
                    //decisions[0].VCode = 8;
                    //decisions[0].TCode = 14;
                }
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 6)
                {
                  FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(-2100, 0, -1200), 0);
                }
              
                /* if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 6)
                 {
                     decisions[0].VCode = 7;
                     decisions[0].TCode = 14;
                 }*/
                 if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 7)
                 {
                    //decisions[0].TCode = 2;
                    FishDecision.FishDecision.MoveToAreaS_2(mission, ref decisions[0], 0, 0, new xna.Vector3(-2100, 0, -1200), 0);
                }
                 if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 8)
                 {
                    FishDecision.FishDecision.MoveToAreaS_2(mission, ref decisions[0], 0, 0, new xna.Vector3(-2100, 0, -1200), 0);
                   // decisions[0].VCode = 8;
                    // decisions[0].TCode = 8;
                 }
                 if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 9)
                 {
                    FishDecision.FishDecision.MoveToAreaS_2(mission, ref decisions[0], 0, 0, new xna.Vector3(-2100, 0, -1200), 0);
                    // decisions[0].VCode = 9;
                    // decisions[0].TCode = 8;
                } 
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 10)
                {
                    FishDecision.FishDecision.Dribble_Will(mission, ref decisions[0], 0, 0, 0, new xna.Vector3(1900, 0, -1200),4,6,7,6,6,9);






                    //获取临时坐标点                                      
                    //xna.Vector3 potision_shang_1 = FishDecision.FishDecision.SetDestPtMm(new xna.Vector3(1900, 0, -1200), mission.EnvRef.Balls[0].PositionMm, 0, 35);


                    //获得目标方向                                   
                    //float mubiaofangxiang = FishDecision.FishDecision.GetDirRad(mission.TeamsRef[0].Fishes[0].PositionMm, new xna.Vector3(1900, 0, -1200));


                    //带球算法Dribble算法

                    //FishDecision.FishDecision.Dribble(ref decisions[0], mission.TeamsRef[0].Fishes[0], potision_shang_1, mubiaofangxiang, (float)Math.PI / 2, (float)Math.PI / 4, 300, 14, 14, 100, 10, true);
                }
                


            }
            
               else
               {
                  ;
                // decisions[0].VCode = 4;
                // decisions[0].TCode = 10;
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 10)
                {
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(924, 0, -230), 180);
                }
                    if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 0)
                {
                    
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(924, 0, -220), 180);
                    //decisions[0].VCode = 3;
                    //decisions[0].TCode = 9;
                }
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 3)
                {
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(-90, 0, 270), 180);
                    //decisions[0].VCode = 4;
                    //decisions[0].TCode = 3;
                }
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 2)
                {
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(-90, 0, 270), 180);
                }
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 5)
                {
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(-1100, 0, -310), 180);
                    //decisions[0].VCode = 4;
                    //decisions[0].TCode = 10;
                }
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 4)
                {
                    FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(-1100, 0, -310), 180);
                }
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 7)
                {
                    FishDecision.FishDecision.MoveToAreaS_2(mission, ref decisions[0], 0, 0, new xna.Vector3(-2100, 0, 1200), 0);
                    //decisions[0].VCode = 8;
                    //decisions[0].TCode = 4;
                }
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 8)
                {
                    FishDecision.FishDecision.MoveToAreaS_2(mission, ref decisions[0], 0, 0, new xna.Vector3(-2100, 0, 1200), 0);
                   // decisions[0].TCode = 6;
                }
                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 9)
                {
                    FishDecision.FishDecision.MoveToAreaS_2(mission, ref decisions[0], 0, 0, new xna.Vector3(-2100, 0, 1200), 0);
                    //decisions[0].TCode = 6;
                }

                if (FishDecision.FishDecision.JudgePIsInArea(mission.TeamsRef[0].Fishes[0].PositionMm) == 12)
                {
                    //获取临时坐标点                                      
                   // xna.Vector3 potision_shang_2 = FishDecision.FishDecision.SetDestPtMm(new xna.Vector3(1900, 0, 1200), mission.EnvRef.Balls[1].PositionMm, 0, 35);


                    //获得目标方向                                    
                    //float mubiaofangxiang = FishDecision.FishDecision.GetDirRad(mission.TeamsRef[0].Fishes[0].PositionMm, new xna.Vector3(1900, 0, 1200));

                    FishDecision.FishDecision.Dribble_Will(mission, ref decisions[0], 0, 0, 1, new xna.Vector3(1900, 0, 1200), 4, 6, 7, 6, 6, 9);
                    //带球算法Dribble算法
                    //FishDecision.FishDecision.Dribble(ref decisions[0], mission.TeamsRef[0].Fishes[0], potision_shang_2, mubiaofangxiang, (float)Math.PI / 2, (float)Math.PI / 4, 300, 14, 14, 100, 10, true);
                }
           }
            return decisions;
        }
    }
}
